import axios from 'axios'
import { Message } from 'element-ui'
import { USER_KEY } from '@/constants/user'
import Router from 'vue-router'
axios.defaults.baseURL = '/service_6002'
axios.interceptors.request.use(config => {
  if (localStorage.getItem(USER_KEY)){
    config.headers['Content-Type'] = 'application/json'
    config.headers.token = localStorage.getItem(USER_KEY)
  }
  return config
})
axios.interceptors.response.use(
  response => {
    if (response.status === 401){
      this.$notify({
        title: '警告',
        message: '需要登录才可以进行操作',
        type: 'warning'
      });
    }
    return response.data
  },
  error => {
    // LoadingManage.closeLoading() // 关闭loading弹层
    let message = ''
    const code = error.response ? error.response.status : ''
    switch (code) {
      case 400:
        message = '请求参数错误'
        break
      case 401:
        message = '需要登录才可进行后续操作'
        break
      case 403:
        message = '操作失败'
        break
      case 404:
        message = '手机号不正确'
        break
      case 500:
        message = '服务器异常'
        break
      case 507:
        message = '服务器数据库异常'
        break
      default :
        message = '处理异常'
    }
    if (message) {
      Message({
        message,
        type: 'warning'
        // onClose: code === '401' ? () => Router.replace({ path: '/啊实打实大所大所多' }) : null
      })
    }
    return new Promise(function () {}) // 终止当前的promise链
  }
)
export default axios
